package fr.umlv.m2.recipes.ideas;

import android.app.Activity;
import android.content.Intent;
import android.content.res.XmlResourceParser;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import fr.umlv.m2.recipes.ideas.features.RecipeDatabaseUtilities;
import fr.umlv.m2.recipes.ideas.features.RecipeGroupFeatures;
import fr.umlv.m2.recipes.ideas.parser.Parser;
import fr.umlv.m2.recipes.ideas.services.SyncService;
import fr.umlv.m2.recipes.ideas.sqlite.db.objects.RecipesDB;
/**
 * Create the Main Activity
 * @author Christophe Chillan
 *
 */
public class MainActivity extends Activity {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splash);
		Intent ServiceIntent = new Intent(this,SyncService.class); 
		startService(ServiceIntent);
		XmlResourceParser xml = getResources().getXml(R.xml.recipes);

		
		
		RecipesDB.createDBInstance(this);
		RecipeGroupFeatures recipesFeatures = Parser.getAllParsedRecipes(xml);

			RecipesDB.open();
			RecipeDatabaseUtilities.createDatabase(recipesFeatures);
			Intent intent = new Intent(this, Splash.class);
			startActivity(intent);
		
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.mainmenu, menu);
		return true;
	}

	/**
	 * Use to Define the Option Menu
	 */
	public boolean onOptionsItemSelected(MenuItem item) {
		// On regarde quel item a été cliqué grâce à son id et on déclenche une
		// action
		switch (item.getItemId()) {
			case R.id.home:
				startActivity(new Intent(this, MenuTabWidget.class));
				return true;
			case R.id.about:
				startActivity(new Intent(this, AboutView.class));
				return true;
			default:
		        return super.onOptionsItemSelected(item);
			}
		
	}

}